#!/usr/bin/env python

import os
import sys
import math
import time
import os.path

if len(sys.argv) != 4 and len(sys.argv) != 5:
    print "Parametry: [plik_z_danymi] [nalepsze_osobniki] [ilosc_kombinacji_na_krok] ([od_ktorego_kroku_zaczac])"
    sys.exit(1)

if os.path.exists(sys.argv[2]) and (len(sys.argv) == 4 or (len(sys.argv) == 5 and int(sys.argv[4]) == 0)):
	print "Plik z najlepszymi juz istnieje"
	sys.exit(1)

if len(sys.argv) == 5:
	begNum = int(sys.argv[4])
else:
	begNum = 0

file = open(sys.argv[1],"r")
num_of_machines = int(file.readline())

i = 0
for line in file:
    i = i + 1

all = num_of_machines ** i

print "ilosc maszyn = ", num_of_machines
print "ilosc operacji = ", i
print "ilosc kombinacji ", all

combinations_per_step = int(sys.argv[3])

begin_time = int(time.time())

while begNum < all:
    command = "java -cp . bforce.BFRunnerParts %s %s %d %d" % (sys.argv[1],sys.argv[2],begNum,combinations_per_step)
    print command
    result = os.system(command)
    if (result != 0):
	    print "BLAD obliczenia przerwane, ponow je od ",begNum
	    sys.exit(1)
    begNum = begNum + combinations_per_step

end_time = int(time.time())
time = end_time - begin_time
time_min = time / 60.0
print "OBLICZENIA trwaly ",time," sekund, czyli ",time_min," minut"
#result = os.system("java -cp . bforce.BFRunnerParts input_5_10_5.txt wynik_5_10_5.txt wynik_gen_5_10_5.txt")
